<?php

namespace JLPay\Models\Openmerch\Device;

use JLPay\Models\BaseResponse;

/**
 * 终端查询响应
 * 
 * @author liaochangyi
 * @since 2025-01-25
 */
class DeviceQueryResponse extends BaseResponse
{
    protected ?array $devices = null;  // 设备列表

    /**
     * 构造函数
     *
     * @param array $data 响应数据
     */
    public function __construct(array $data)
    {
        parent::__construct($data);
        
        // 处理设备列表
        $devicesData = $data['devices'] ?? [];
        if ($devicesData && is_array($devicesData)) {
            $this->devices = [];
            foreach ($devicesData as $device) {
                $this->devices[] = new DeviceInfo($device);
            }
        }
    }

    /**
     * 获取设备列表
     *
     * @return DeviceInfo[]|null
     */
    public function getDevices(): ?array
    {
        return $this->devices;
    }

    /**
     * 转换为数组
     *
     * @return array
     */
    public function toArray(): array
    {
        $data = parent::toArray();
        
        // 转换设备列表
        if ($this->devices) {
            $data['devices'] = [];
            foreach ($this->devices as $device) {
                $data['devices'][] = $device->toArray();
            }
        } else {
            $data['devices'] = null;
        }
        
        return $data;
    }
} 